# $Id: models.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# django imports
from django.db import models

# local imports
from djanet.lib import exception, ip, stdlib
from djanet.db import models as model

# Create your models here.
class bgpPrefixList(model.BaseModel):
   prefixListName = models.CharField(max_length = 24,
         help_text = "Prefix list name", name = "prefixlist")
   prefixListSeq = models.IntegerField(
         help_text = "Prefix list sequence number", name = "sequence")
   device = models.ForeignKey(dbmodels.Device)
   version = models.CharField(max_length = 10, choices = dbchoices.ADDR_FAMILY,
         help_text = "IP address family")

   class Meta:
      db_table = u'bgp_prefixlist'
      ordering = ['prefixListName']

   def __str__(self):
      return str(self.prefixListName)

   Device = property(fget = lambda self: self.device)

class allowedPrefixes(model.BaseModel):
   address = model.IPAddressField(help_text = "IP address", name = "address")
   prefix = models.IntegerField(help_text = "Prefix value for the address",
         name = "prefix")
   prefixList = models.ForeignKey(bgpPrefixList)

   class Meta:
      db_table = u'bgp_allowedprefix'
      ordering = ['prefixList']

   def __str__(self):
      return str(self.address)

class bgpRouteMap(model.RouteMap):
   localPref = models.IntegerField(null = True, blank = True,
         default = 100, help_text = "BGP Local preference for a route",
         name = "local_pref")
   asPath = models.IntegerField(null = True, blank = True,
         help_text = "AS path for a bgp route", name = "as_path")
   prefixList = models.ForeignKey(bgpPrefixList, null = True, blank = True)
   address = models.ForeignKey(fwmodels.IPAccessList, null = True,
         blank = True)
   redist = models.BooleanField(
         help_text = "Route map used for redistribution")

   class Meta:
      db_table = u'bgp_rtemap'
